package com.wanhe.viewconf.views.child;

import android.content.Context;
import android.graphics.Color;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import com.wanhe.base.model.base.Dish;
import com.wanhe.base.model.disgn.Event;
import com.wanhe.base.model.disgn.ViewEvent;
import com.wanhe.base.model.disgn.ViewItem;
import com.wanhe.base.model.disgn.ViewModel;
import com.wanhe.base.util.ObjectUtil;
import com.wanhe.base.util.SPUtils;
import com.wanhe.viewconf.views.group.CoordinateViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * File Name:MealBookChildItemView.java<br/>
 * Package Name:com.wanhe.viewconf.views.child<br/>
 * Date:2017/7/11 15:26<br/>
 * author:chendeji<br/>
 * desc: 最基础的菜单子控件，里面什么东西都没有，空白透明<br/>
 * Copyright (c) 2017/7/11, 781571323@qq.com All Rights Reserved.
 *
 */
public abstract class MealBookChildItemView extends BaseXYView {

	public static final String TAG = "MealBookChildItemView";

	protected Dish		mDish;
	protected ViewModel	mModel;
	protected int		mPagePosition;
	protected String	mColor;
	protected ViewItem	mItem;

	protected Animation animationIn;
	protected Animation animationOut;

	//进场动画监听
	private Animation.AnimationListener inListener = new Animation.AnimationListener() {
		@Override
		public void onAnimationStart(Animation animation) {

		}

		@Override
		public void onAnimationEnd(Animation animation) {
			setVisibility(VISIBLE);
		}

		@Override
		public void onAnimationRepeat(Animation animation) {

		}
	};

	//出场动画监听
	private Animation.AnimationListener outListener = new Animation.AnimationListener() {
		@Override
		public void onAnimationStart(Animation animation) {

		}

		@Override
		public void onAnimationEnd(Animation animation) {
			setVisibility(INVISIBLE);
		}

		@Override
		public void onAnimationRepeat(Animation animation) {

		}
	};

	public MealBookChildItemView(Context context) {
		super(context);
	}

	/**
	 * Methor Name:bindData<br/>
	 * Package Name:com.wanhe.viewconf.views<br/>
	 * Date:2017/7/4 16:06<br/>
	 * author:chendeji<br/>
	 * desc: 绑定菜品的数据<br/>
	 * Copyright (c) 2017/7/4, 781571323@qq.com All Rights Reserved.
	 *
	 * @param dish
	 *            菜品对象
	 * @return
	 *
	 */
	public void bindData(Dish dish) {
		this.mDish = dish;
	}

	/**
	 * @deprecated
	 * 			Methor Name:initViewModel<br/>
	 *             Package Name:com.wanhe.viewconf.views<br/>
	 *             Date:2017/7/4 16:07<br/>
	 *             author:chendeji<br/>
	 *             desc: 绑定设计器这个子View的一些视图信息<br/>
	 *             Copyright (c) 2017/7/4, 781571323@qq.com All Rights Reserved.
	 *
	 * @param model
	 *            最基本的子控件信息
	 * @return
	 *
	 */
	public void initViewModel(ViewModel model) {
		this.mModel = model;
	}

	public void initModelData(ViewItem item) {
		this.mItem = item;
		if (item.isVisible()){
			setVisibility(VISIBLE);
		} else {
			setVisibility(INVISIBLE);
		}
	}

	/**
	 * Methor Name:setPagePosition<br/>
	 * Package Name:com.wanhe.viewconf.views<br/>
	 * Date:2017/7/4 16:12<br/>
	 * author:chendeji<br/>
	 * desc: 设置控件所在的具体页面位置<br/>
	 * Copyright (c) 2017/7/4, 781571323@qq.com All Rights Reserved.
	 *
	 * @param pagePosition
	 *            控件所在的页面位置
	 * @return
	 *
	 */
	public void setPagePosition(int pagePosition) {
		this.mPagePosition = pagePosition;
	}

	public int getPagePosition() {
		return this.mPagePosition;
	}

	public Dish getDish() {
		return mDish;
	}

	public ViewModel getModel() {
		return mModel;
	}

	public int getColor() {
		int hexColor = 0;
		try {
			hexColor = Color.parseColor(this.mColor);
		} catch (Exception e) {
			hexColor = Color.BLACK;
		}
		return hexColor;
	}

	public ViewItem getItem() {
		return mItem;
	}

	/**
	 * Methor Name:setViewColor<br/>
	 * Package Name:com.wanhe.viewconf.views<br/>
	 * Date:2017/7/4 16:12<br/>
	 * author:chendeji<br/>
	 * desc: 设置控件中文本的颜色<br/>
	 * Copyright (c) 2017/7/4, 781571323@qq.com All Rights Reserved.
	 *
	 * @param color
	 *            字体颜色的字符串信息
	 * @return
	 *
	 */
	public void setViewColor(String color) {
		this.mColor = color;
	}

	/**
	 * Methor Name:sendEvents<br/>
	 * Package Name:com.wanhe.viewconf.views.child<br/>
	 * Date:2017/7/11 15:34<br/>
	 * author:chendeji<br/>
	 * desc: 子控件都可以调用改方法将事件通过父控件传递给其他的子控件来处理<br/>
	 * Copyright (c) 2017/7/11, 781571323@qq.com All Rights Reserved.
	 *
	 * @param events
	 *            {@link Event} 原始事件的数组
	 * @return
	 */
	protected void sendEvents(List<Event> events) {
		ViewGroup parent = (ViewGroup) this.getParent();
		if (parent != null && parent instanceof CoordinateViewGroup) {
			ViewEvent event = new ViewEvent();
			event.setFromCid(this.mItem.getCid());
			event.mEvents = events;
			event.dish = this.mDish;
			event.coor = getCoor();
			CoordinateViewGroup group = (CoordinateViewGroup) parent;
			group.receiveEvent(event);
		}
	}

	/**
	 * Methor Name:sendEvents<br/>
	 * Package Name:com.wanhe.viewconf.views.child<br/>
	 * Date:2017/7/11 15:34<br/>
	 * author:chendeji<br/>
	 * desc: 发送单个事件<br/>
	 * Copyright (c) 2017/7/11, 781571323@qq.com All Rights Reserved.
	 *
	 * @param e
	 *            {@link Event} 原始事件
	 * @return
	 */
	protected void sendEvent(Event e){
		List<Event> events = new ArrayList<>();
		events.add(e);
		ViewGroup parent = (ViewGroup) this.getParent();
		if (parent != null && parent instanceof CoordinateViewGroup) {
			ViewEvent event = new ViewEvent();
			event.setFromCid(this.mItem.getCid());
			event.mEvents = events;
			event.dish = this.mDish;
			event.coor = getCoor();
			CoordinateViewGroup group = (CoordinateViewGroup) parent;
			group.receiveEvent(event);
		}
	}

	/**
	 * Methor Name:getCoor<br/>
	 * Package Name:com.wanhe.viewconf.views.child<br/>
	 * Date:2017/7/11 15:48<br/>
	 * author:chendeji<br/>
	 * desc: 获取控件的坐标信息以及控件的长宽信息<br/>
	 * Copyright (c) 2017/7/11, 781571323@qq.com All Rights Reserved.
	 *
	 * @param
	 * @return {@link com.wanhe.base.model.disgn.ViewEvent.Coor}
	 *
	 */
	protected ViewEvent.Coor getCoor() {
		ViewEvent.Coor coor = new ViewEvent.Coor();
		coor.l = getLeft();
		coor.t = getTop();
		coor.r = getRight();
		coor.b = getBottom();
		return coor;
	}

	/**
	 * Methor Name:getViewXY<br/>
	 * Package Name:com.wanhe.viewconf.views.child<br/>
	 * Date:2017/7/11 15:49<br/>
	 * author:chendeji<br/>
	 * desc: 获取控件本身在屏幕上的具体坐标位置<br/>
	 * Copyright (c) 2017/7/11, 781571323@qq.com All Rights Reserved.
	 *
	 * @param
	 * @return
	 *
	 */
	protected int[] getViewXY() {
		int[] xy = new int[2];
		getLocationOnScreen(xy);
		return xy;
	}

	private String getHostAddress() {
		return "http://" + SPUtils.getIp() + ":" + SPUtils.getPort();
	}

	@Override
	public void show(Event event, ViewEvent.Coor coor) {
		/**
		 * 如果有动画需要执行，执行动画
		 * @author 陈德基
		 * @time 2017/8/3 14:36
		 */
		if (ObjectUtil.checkObject(animationIn)){
			animationIn.setAnimationListener(inListener);
			animationIn.setDuration(this.getItem().getDurationIn());
			this.startAnimation(animationIn);
		} else {
			setVisibility(VISIBLE);
		}
	}

	@Override
	public void pause(Event event, ViewEvent.Coor coor) {
	}

	@Override
	public void resume(Event event, ViewEvent.Coor coor) {
	}

	@Override
	public void hide(Event event, ViewEvent.Coor coor) {
		/**
		 * 如果有动画需要执行，执行动画
		 * @author 陈德基
		 * @time 2017/8/3 14:36
		 */
		if (ObjectUtil.checkObject(animationOut)){
			animationOut.setAnimationListener(outListener);
			animationOut.setDuration(this.getItem().getDurationOut());
			this.startAnimation(animationOut);
		} else {
			setVisibility(INVISIBLE);
		}
	}

	@Override
	public void tap(Event event, ViewEvent.Coor coor) {

	}

	@Override
	public void set(Event event, ViewEvent.Coor coor) {
	}

	/**
	 * Methor Name:callSendEventMsg<br/>
	 * Package Name:com.wanhe.viewconf.views.child<br/>
	 * Date:2017/7/26 9:25<br/>
	 * author:chendeji<br/>
	 * desc: 发送该控件本身携带的事件<br/>
	 * Copyright (c) 2017/7/26, 781571323@qq.com All Rights Reserved.
	 *
	 * @param
	 * @return
	 *
	 */
	protected void callSendEventMsg() {
		ViewItem mItem = this.mItem;
		List<Event> events = mItem.getEvents();
		if (ObjectUtil.checkObjectList(events)) {
			// 如果这个hit控件有事件能够发送那么就要将事件发送出去
			sendEvents(events);
		}
	}

	/**
	 * Methor Name:sendEventWithKey<br/>
	 * Package Name:com.wanhe.viewconf.views.child<br/>
	 * Date:2017/8/8 18:41<br/>
	 * author:chendeji<br/>
	 * desc: 发送指定的事件<br/>
	 * Copyright (c) 2017/8/8, 781571323@qq.com All Rights Reserved.
	 *
	 * @param key
	 * @return
	 *
	 */
	public void sendEventWithKey(String key){
		List<Event> events = this.mItem.getEvents();
		if (ObjectUtil.checkObjectList(events)) {
			for (int i = 0; i < events.size(); i++) {
				String event = events.get(i).getEvent();
				if (event.equals(key)){
					sendEvent(events.get(i));
					break;
				}
			}
		}
	}

	/**
	 * Methor Name:callSelfEvent<br/>
	 * Package Name:com.wanhe.viewconf.views.child<br/>
	 * Date:2017/7/26 16:48<br/>
	 * author:chendeji<br/>
	 * desc: 执行自身事件的方法<br/>
	 * Copyright (c) 2017/7/26, 781571323@qq.com All Rights Reserved.
	 *
	 * @param
	 * @return
	 *
	 */
	public void callSelfEvent(String key){
		List<Event> events = this.mItem.getEvents();
		if (ObjectUtil.checkObjectList(events)) {
			for (int i = 0; i < events.size(); i++) {
				String event = events.get(i).getEvent();
				if (event.equals(key)){
					onEvent(key, events.get(i), getCoor());
				}
			}
		} else {
			onEvent(key, null, getCoor());
		}
	}

	/**
	 * Methor Name:bindAnimateObj<br/>
	 * Package Name:com.wanhe.viewconf.views.child<br/>
	 * Date:2017/8/3 14:30<br/>
	 * author:chendeji<br/>
	 * desc: 进行绑定动画的操作<br/>
	 * Copyright (c) 2017/8/3, 781571323@qq.com All Rights Reserved.
	 *
	 * @param event 带有动画绑定信息的事件
	 * @return
	 *
	 */
	protected void bindAnimateObj(Event event) {
		int[] animateSet = (int[]) event.getArgs().getCusObj();
		int animateIn = animateSet[0];
		int animateOut = animateSet[1];

		this.animationIn = AnimationUtils.loadAnimation(getContext(), animateIn);
		this.animationOut = AnimationUtils.loadAnimation(getContext(), animateOut);
	}
}
